import axios from 'axios'
import qs from 'qs'
import {message} from 'antd'

export default function ajax(url, data={}, method='GET'){
  return new Promise(function (resolve, reject){
    let promise
    if(method === 'GET'){
      promise = axios.get(url, {params: data})
    } else if(method === 'POST'){
      promise = axios.post(url, qs.stringify(data))
    }
    promise.then(response => {
      if(response.data.code !== 0){
        message.error(response.data.msg)
      }
      resolve(response.data)
    }).catch(error => {
      message.error('请求错误' + error.msg)
    })
  })
}